<?php
/**
 * Created by PhpStorm.
 * User: zws
 * Date: 2018/9/8
 * Time: 11:02
 * 检测报告管理
 */
namespace app\admin\service;

use app\common\model\ReportModel;
class ReportService
{
    protected static $model;

    /**
     * InstrumentService constructor.
     * @param ReportModel $reportModel
     * @author zws
     */

    public function __construct(ReportModel $reportModel)
    {
        self::$model = $reportModel;
    }

    /**
     * 检测报告列表
     * @param $param
     * @return array|\PDOStatement|string|\think\Collection
     * @author zws
     */
    public function index($param)
    {
        if(!isset($param['page']) || empty($param['page'])){
            $param['page'] = 1;
        }
        if(!isset($param['limit']) || empty($param['limit'])){
            $param['limit'] = 10;
        }
        $where = ['is_del' => 1];
        // 获取总数
        $count = self::$model->where($where)->count();
        if ($count < 1) {
            return ['data' => [], 'count' => $count];
        }
        $result = self::$model->where($where)
            ->page($param['page'], $param['limit'])
            ->order('id', 'asc')
            ->select();
        $result->map(function (&$item) {
            $item->user;
            $item->orders;
            $item->report_url = config('app.app_host'). ltrim(config('param_config.reportPath'), '.') . $item->report_url;
            return $item;
        });
        return ['data' => $result, 'count' => $count];
    }

    /**
     * 删除检测报告
     * @param $param
     * @return bool
     * @author zws
     * @throws \Exception
     */
    public function update($param)
    {
        if(empty($param) || !isset($param['id']) || empty($param['id']) || !isset($param['is_del']) || ($param['is_del'] != 2))
        {
            app_fail(9997);//参数错误
        }
        try{
            $result = self::$model->save(['is_del'=> 2 ], ['id' => $param['id']]);
//            $result = self::$model->delete($param['id']);
            return $result;
        }catch (Exception $e){
            app_fail(9899);//仪器编辑失败
        }
    }
}